#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <arpa/inet.h>

#include "ak_common.h"
#include "ak_csrv.h"

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#define SYSIPC_PORT     (8899)	    //sysipc communication port
#define ARG_NUM_MIN		(2)

/*
 * usage: show this program usage
 */
static void usage(void)
{
	printf("need %d argment at lease\n", ARG_NUM_MIN);
	printf("option: \n"
		   "[--file] file\n"
		   "[-t --tips]\n"
		   "[--ircut] [0/1]\n"
		   "[--photosensitive] [0/1]\n"
		   "[--its] [0/1]\n");
}

/*
 * connect_sysipc - connect ipc service
 * @sock[IN]: return by init
 * return: 0 on success, -1 failed
 */
static int connect_sysipc(int sock)
{
	struct sockaddr_in addr;
	socklen_t addrlen = sizeof(struct sockaddr_in);

	memset(&addr, 0, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(SYSIPC_PORT);
	addr.sin_addr.s_addr = inet_addr("127.0.0.1");

	/* do connect */
	if (connect(sock, (struct sockaddr *)&addr, addrlen) == -1) {
		printf("error connect: %s\n", strerror(errno));
		return AK_FAILED;
	}

	return AK_SUCCESS;
}

/*
 * send cmd - send command by socket to cmd server
 * cmd[IN]: command string pointer to the command which you want to send
 * len[IN]: command length
 * return: non
 */
static void send_cmd(const char *cmd, int len)
{
	int sock = ak_csrv_sysipc_init();
	if (sock <= 0) {
		printf("socket create failed\n");
		return ;
	}

	connect_sysipc(sock);

	printf("send cmd: %s\n", cmd);
	send(sock, cmd, len, 0);

	ak_csrv_sysipc_exit(sock);
}

int main(int argc, char **argv)
{
	if (argc < ARG_NUM_MIN) {
		usage();
		return 0;
	}

	/*
	 * arguments table
	 */
	int opt;
	const struct option longopts[] = {
		{"file", 1, NULL, 'f'},		//file
		{"tips", 0, NULL, 't'},		//tip voice
		{"ircut", 1, NULL, 'r'},	//ircut
		{"photosensitive", 1, NULL, 'p'},	//photosensitive
		{"its", 1, NULL, 'i'},	//its
		{0, 0, 0, 0}				//end
	};
	char cmd[255] = {0};
	char file[255] = {0};
	int play_tips = 0;
	int ircut_switch = 0;
	int ircut_state = 0;
	int photosensitive_enable = -1;
	int its_start = -1;

	/* to parse arguments */
	while ((opt = getopt_long(argc, argv, ":frtpi", longopts, NULL)) != -1) {
		switch (opt) {
		case ':':
			printf("need a value to specifica file\n");
			break;
		case 'f': /* match f or file */
			printf("get file: %s\n", optarg);
			if (optarg)
				strcpy(file, optarg);
			break;
		case 't': /* match t or tips */
			printf("you will play tips\n");
			play_tips = 1;
			break;
		case 'r': /* match r or ircut */
			printf("you will set ircut, arg: %s\n", optarg);
			ircut_switch = 1;
			ircut_state = atoi(optarg);
			break;
		case 'p': /* match p or photosensitive */
			photosensitive_enable = atoi(optarg);
			printf("you want to set photosensitive %s\n",
					photosensitive_enable == 1 ? "enable" : "disable");
			break;
		case 'i': /* match i or its */
			its_start = atoi(optarg);
			printf("you want to set its %s\n",
					its_start == 1 ? "start" : "stop");
			break;
		default:
			usage();
			break;
		}
	}

	/*
	 * construct command and send out
	 */
	int len = 0;
	/* this is play voice tips */
	if (play_tips && (strlen(file) > 0)) {
		sprintf(cmd, "%s,%s", "tips", file);
		len = strlen(cmd) + 1;
		send_cmd(cmd, len);
	}

	/* this is switch ircut and video day-night mode */
	if (ircut_switch) {
		sprintf(cmd, "%s,%d", "ircut", ircut_state);
		len = strlen(cmd) + 1;
		send_cmd(cmd, len);
	}

	/*
	 * internal auto switch ircut by photosensitive
	 * enable or disable switch
	 */
	if (photosensitive_enable != -1) {
		sprintf(cmd, "%s,%d", "photosensitive", photosensitive_enable);
		len = strlen(cmd) + 1;
		send_cmd(cmd, len);
	}

	/* isp tool start or stop */
	if (its_start != -1) {
		sprintf(cmd, "%s,%d", "its", its_start);
		len = strlen(cmd) + 1;
		send_cmd(cmd, len);
	}

	return 0;
}
